#include "GenPoints.h"
#include <random>
#include <fstream>

namespace rock {
	GenPoints::GenPoints(plane_type plane, double cov) : plane_ {plane}, cov_ {cov} {

	}

	auto GenPoints::generate() -> points_type {
		std::normal_distribution<double> nd(0.0, std::sqrt(cov_));
		std::random_device               rd;
		std::default_random_engine       rng {rd()};

		points_type all_points {};

		for (int x = -PointNum / 2; x < PointNum / 2; x++) {
			for (int y = -PointNum / 2; y < PointNum / 2; y++) {
				double px = x + nd(rng);
				double py = y + nd(rng);
				double pz = (plane_.x() * px + plane_.y() * py + plane_.w()) / (-1 * plane_.z()) + nd(rng);

				all_points.emplace_back(point_type {px, py, pz});
			}
		}

		return all_points;
	}


	void GenPoints::save(points_type pts) {
		std::string   name = "points.txt";
		std::ofstream of_points(name, std::ios::trunc);
		of_points << "# variance: " << cov_ << std::endl;
		of_points << "# plane: " << plane_.x() << "x + " << plane_.y() << "y + " << plane_.z() << "z + " << plane_.w()
		          << " = 0" << std::endl;
		for (point_type& p : pts) {
			of_points << p.x() << " " << p.y() << " " << p.z() << std::endl;
		}
		of_points.close();
	}
}